<?php 

include("../conn.php");

function checkVal($key,$msg){

	if(isset($_POST[$key])){
		return $_POST[$key];
	}

	echo json_encode(array('code'=>-100,'msg'=>$msg.'异常'));
	exit;
}

$userid = null;
$invoice_id = null;
$unit_name = checkVal('unit_name','单位名称');
$code = checkVal('code','纳税人识别码');
$address = checkVal('address','注册地址');
$tel = checkVal('tel','电话');
$banks = checkVal('banks','开户银行');
$banks_account = checkVal('banks_account','银行账户');
if(isset($_POST['invoice_id']) && $_POST['invoice_id'] != ''){
	$invoice_id = $_POST['invoice_id'];
}else{
	$userid = checkVal('userid','用户信息');
}

if($invoice_id){
	$sql = 'update user_invoice set `unit_name` = "'.$unit_name.'",`code` = "'.$code.'",`address` = "'.$address.'",`tel` = "'.$tel.'",`banks` = "'.$banks_account.'",`banks` = "'.$banks_account.'"';

	$query = mysql_query($sql);
	if($query){
		echo json_encode(array('code'=>200,'msg'=>'保存成功'
			,'data'=>array('id'=>$invoice_id,'unit_name'=>$unit_name,'code'=>$code,'address'=>$address,'tel'=>$tel,'banks'=>$banks,'banks_account'=>$banks_account,'userid'=>$userid)));
	exit;
	}
}else{
	$sql = 'insert into user_invoice(`unit_name`,`code`,`address`,`tel`,`banks`,`banks_account`,`userid`) values("'.$unit_name.'","'.$code.'","'.$address.'","'.$tel.'","'.$banks.'","'.$banks_account.'","'.$userid.'")';
	$query = mysql_query($sql);
	$insertid = mysql_insert_id();
	if($query && $insertid){
		echo json_encode(array('code'=>200,'msg'=>'保存成功','data'=>array('id'=>$insertid,'unit_name'=>$unit_name,'code'=>$code,'address'=>$address,'tel'=>$tel,'banks'=>$banks,'banks_account'=>$banks_account,'userid'=>$userid)));
		exit;
	}
}
echo json_encode(array('code'=>-100,'msg'=>'保存失败'));









 ?>